package org.black.phoneUtility.sms.reply.app.activity;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;

public class RingtoneChangeActivity extends Activity {
	public static final String TAG = RingtoneChangeActivity.class.getName();

	private class RingtoneChangeDialogFragment extends DialogFragment implements
			DialogInterface.OnClickListener {
		public final String TAG = RingtoneChangeDialogFragment.class.getName();
		private boolean clickPositiveButton = false;

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder.setTitle(R.string.sms_reply_title)
					.setMessage(R.string.sms_reply_ring_tone_change_dialog_body)
					.setPositiveButton(android.R.string.ok, this)
					.setNegativeButton(android.R.string.cancel, this);
			return builder.create();
		}

		@Override
		public void onClick(DialogInterface dialog, int which) {
			if (which == DialogInterface.BUTTON_POSITIVE) {
				SharedPreferences preferences = PreferenceManager
						.getDefaultSharedPreferences(getApplicationContext());
				String key = getApplicationContext().getString(
						R.string.preference_sms_reply_original_ringer_mode);
				AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
				preferences.edit().putInt(key, audioManager.getRingerMode())
						.commit();
				audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
				this.clickPositiveButton = true;
			} else {
				this.removePreference();
			}
			finish();
		}

		@Override
		public void onCancel(DialogInterface dialog) {
			super.onCancel(dialog);
			this.removePreference();
			finish();
		}

		@Override
		public void onDismiss(DialogInterface dialog) {
			super.onDismiss(dialog);
			this.removePreference();
			finish();
		}

		private void removePreference() {
			if (this.clickPositiveButton == false) {
				SharedPreferences preferences = PreferenceManager
						.getDefaultSharedPreferences(getApplicationContext());
				String key = getApplicationContext().getString(
						R.string.preference_sms_reply_original_ringer_mode);
				preferences.edit().remove(key).commit();
			}
		}
	}

	private RingtoneChangeDialogFragment ringtoneChangeDialogFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ringtoneChangeDialogFragment = new RingtoneChangeDialogFragment();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.ringtoneChangeDialogFragment = null;
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.ringtoneChangeDialogFragment.dismiss();
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.ringtoneChangeDialogFragment.show(getFragmentManager(),
				ringtoneChangeDialogFragment.TAG);
	}

}
